const err = require('./error.js')
const sys = require('./sys.js')

class LRUItem {
	key;
	accessCount = 0;
	accessIndex = 0;
	constructor(key) {
		this.key = key
	}
}

class LRUMap {
	constructor(contentSize) {
		this.maxSize = contentSize
	}
	g() {}
}

function memory(cache, func) {
	return function() {
		let [forceUpdate, args] = Array.from(arguments)
		let argKeys = args.join('Amitabha')
		if (!forceUpdate)
			var result = func(args)
	}
}

class CacheMap {
	constructor(key) {
		this.cacheKey = key
	}
	initDone() {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) return true
		else return false
	}
	has(key) {
		if (this.get(key)) return true
		else return false
	}
	get(key) {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) {
			return obj[key]
		} else {
			return undefined
		}
	}
	set(key, value) {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) {
			obj[key] = value
		} else {
			obj = {}
			obj[key] = value
		}
		sys.set_cache(this.cacheKey, obj)
	}
	remove(key) {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) {
			delete obj[key]
			sys.set_cache(this.cacheKey, obj)
		}
	}
	getKeys() {
		var obj = sys.get_cache(this.cacheKey)
		let result = []
		if (obj) {
			for (let key in obj) {
				result.push(key)
			}
		}
		return result
	}
	setAllData(obj) {
		sys.set_cache(this.cacheKey, obj)
	}
	clear() {
		sys.rm_cache(this.cacheKey)
	}
}

class CacheSet {
	constructor(key) {
		this.cacheKey = key
	}
	initDone() {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) return true
		else return false
	}
	asArray() {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) return obj
		else return []
	}
	setValues(list) {
		sys.set_cache(this.cacheKey, list)
	}
	add(value) {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) {
			let s = new Set(obj)
			s.add(value)
			obj = [...s]
		} else {
			obj = []
			obj.push(value)
		}
		sys.set_cache(this.cacheKey, obj)
	}
	has(value) {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) {
			return new Set(obj).has(value)
		} else {
			return false
		}
	}
	rm(value) {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) {
			let s = new Set(obj)
			if (s.delete(value)) {
				obj = [...s]
				sys.set_cache(this.cacheKey, obj)
				return obj
			} else {
				return undefined
			}
		} else {
			sys.set_cache(this.cacheKey, [])
			return undefined
		}
	}
	clear() {
		sys.rm_cache(this.cacheKey)
	}
}

class CacheList {
	constructor(key) {
		this.cacheKey = key
	}
	initDone() {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) return true
		else return false
	}
	getList() {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) return obj
		else return []
	}
	setList(list) {
		sys.set_cache(this.cacheKey, list)
	}
	push(value) {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) {
			obj.push(value)
		} else {
			obj = []
			obj.push(value)
		}
		sys.set_cache(this.cacheKey, obj)
		return obj
	}
	unshift(value) {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) {
			obj.unshift(value)
		} else {
			obj = [value]
		}
		sys.set_cache(this.cacheKey, obj)
	}
	pop() {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) {
			const item = obj.pop()
			sys.set_cache(this.cacheKey, obj)
			return obj.pop()
		} else {
			sys.set_cache(this.cacheKey, [])
			return undefined
		}
	}
	filter(func) {
		var obj = sys.get_cache(this.cacheKey)
		if (obj) {
			const result = obj.filter(func)
			sys.set_cache(this.cacheKey, result)
			return result
		} else {
			return []
		}
	}
	clear() {
		sys.rm_cache(this.cacheKey)
	}
}


module.exports = {
	CacheMap: CacheMap,
	CacheList: CacheList,
	CacheSet: CacheSet,
	LRUMap: LRUMap,
}
